import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('@/views/admin/AdminLogin.vue')
  },
  {
    path: '/admin',
    name: 'AdminLayout',
    component: () => import('@/views/admin/AdminLayout.vue'),
    redirect: '/admin/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/admin/AdminDashboard.vue')
      },
      {
        path: 'posts',
        name: 'Posts',
        component: () => import('@/views/admin/AdminPosts.vue')
      },
      {
        path: 'comments',
        name: 'Comments',
        component: () => import('@/views/admin/AdminComments.vue')
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/admin/AdminUsers.vue')
      },
      {
        path: 'blacklist',
        name: 'Blacklist',
        component: () => import('@/views/admin/AdminBlacklist.vue')
      },
      {
        path: 'review',
        name: 'Review',
        component: () => import('@/views/admin/AdminReview.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/admin/login'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫，处理登录验证
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('adminToken')
  
  if (to.path !== '/admin/login' && !isLoggedIn) {
    next({ path: '/admin/login' })
  } else {
    next()
  }
})

export default router 